package com.example.mallproduct.controller;

import com.common.utils.PageUtils;
import com.common.utils.R;
import com.example.mallproduct.config.ProductConfig;
import com.example.mallproduct.controller.vo.AttrVO;
import com.example.mallproduct.entity.AttrEntity;
import com.example.mallproduct.service.AttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品属性
 *
 * @author charlie
 * @email @gmail.com
 * @date 2023-11-26 14:55:58
 */
@RestController
@RequestMapping("mallproduct/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/sale/list")
    public R saleList(@RequestParam Map<String, Object> params) {
        System.out.println("测试");
        System.out.println(params);
        PageUtils page = attrService.queryPage(params);


        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId) {
        AttrEntity attr = attrService.info(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVO attrVO) {
        attrService.saveAttr(attrVO);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVO attr) {
        attrService.updateBaseAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

    @Autowired
    private ProductConfig productConfig;

    @RequestMapping("/test")
    public R test() {

//        R r = R.ok();
//        r.put("TestCode", productConfig.getTestCode());
//        r.put("Bramk", productConfig.getBramk());

        List<AttrEntity> page = attrService.list();

        page.forEach(attrEntity -> {
            System.out.println(attrEntity.getAttrName());
        });

        return R.ok().put("page", page);
    }


    @RequestMapping("/category/info/{categoryId}")
    public R infoByCategoryId(@PathVariable("categoryId") Long categoryId) {
        PageUtils page = attrService.infoByCategoryId(categoryId);

        return R.ok().put("page", page);
    }

}
